<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Layout Class
 *
 * This class manages the view insertion into a layout template
 *
 * @package		Layout
 * @version		1.0
 * @author 		Richard Davey <info@richarddavey.com>
 * @copyright 	Copyright (c) 2011, Richard Davey
 * @link		https://github.com/richarddavey/codeigniter-layout
 */
class Layout {
	
	/**
	 * Default values
	 *
     */
	private $active             = TRUE;    
	private $layout             = 'default';
	private $layout_var         = 'content';
    private $layout_title       = 'MainTitle';
    private $layout_keyword     = 'MainKeyword';
    private $layout_description = 'MainDescription';
    private $layout_dir         = 'layouts/';
    private $varRel              = 'pgRel';
    private $varJavascript       = 'pgJavascript';
    private $clean_output       = FALSE;
    
    // config title
    // @author hungtd
    // title set from controller
    private $title          = '';
    private $keyword        = '';
    private $description    = '';
    private $pgRel          = '';
    private $pgJavascript   = '';

        /**
	 * Constructor
	 *
	 * @access	public
	 * @param	array	initialization parameters
	 */
	public function __construct($params = array())
	{
		if (count($params) > 0)
		{
			$this->initialize($params);
		}
		
		log_message('debug', "Layout Class Initialized");
	}

	// --------------------------------------------------------------------

	/**
	 * Initialize Preferences
	 *
	 * @access	public
	 * @param	array	initialization parameters
	 * @return	void
	 */
	private function initialize($params = array())
	{
		if (count($params) > 0)
		{
			foreach ($params as $key => $val)
			{
				if (isset($this->$key))
				{
					$this->$key = $val;
				}
			}
		}
	}
	
	// --------------------------------------------------------------------

	/**
	 * Getter method for private vars
	 *
	 * @param 	string
	 * @return 	mixed
	 */
	public function __get($name) 
	{
		// get variable
		return isset($this->$name) ? $this->$name : NULL;
	}
	
	// --------------------------------------------------------------------

	/**
	 * Disable output cleaning
	 *
	 * @return 	void
	 */
	public function disable_clean_output()
	{
		// disable layout
		$this->clean_output = FALSE;
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Disable layout
	 *
	 * @return 	void
	 */
	public function disable_layout()
	{
		// disable layout
		$this->active = FALSE;
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Enable layout
	 *
	 * @return	void
	 */
	public function enable_layout()
	{
		// enable layout
		$this->active = TRUE;
	}
	
	// --------------------------------------------------------------------

	/**
	 * Set layout
	 *
	 * @param 	string $layout
	 * @return 	void
	 */
	public function set_layout($layout)
	{
		// set layout
		$this->layout = $layout;
	}
	
	// --------------------------------------------------------------------

	/**
	 * Set layout directory
	 *
	 * @param 	string $layout_dir
	 * @return 	void
	 */
	public function set_layout_dir($layout_dir)
	{
		// set layout directory
		$this->layout_dir = $layout_dir;
	}

	// --------------------------------------------------------------------

	/**
	 * Output with layout
	 *
	 * Remove useless whitespace from generated HTML and construct page from
	 * template with layout and current set output
	 *
	 * @return 	void
	 */
	public function output()
	{
		// set up CI classes
		$CI =& get_instance();
		
		// get output
		$buffer = $CI->output->get_output();
			
		// does layout exist
		if ($CI->layout->active AND $CI->layout->layout AND file_exists(APPPATH . $CI->layout->layout_dir . $CI->layout->layout . EXT)) {
		
			// return layout
			$buffer = $CI->load->view('../' . $CI->layout->layout_dir . $CI->layout->layout . EXT, array(
                $CI->layout->layout_var             => $buffer, 
                $CI->layout->layout_title           => $CI->layout->get_title(),
                $CI->layout->layout_keyword         => $CI->layout->get_keyword(),
                $CI->layout->layout_description     => $CI->layout->get_description(),
                $CI->layout->varRel                 => $CI->layout->get_rel(),
                $CI->layout->varJavascript          => $CI->layout->get_javascript(),
            ), TRUE);
		}
		
		// if whitespace compression is needed
		if ($CI->layout->clean_output) {
			
			// search
			$search = array(
				'/\>[^\S \t]+/s', 				// strip whitespaces after tags, except space and tab
			    '/[^\S ]+\</s',					// strip whitespaces before tags, except space
			    '/(\s)+/s',    					// shorten multiple whitespace sequences
			    '#\/\/\<\!\[CDATA\[[^\S ]+#s'	// strip whitespace directly after CDATA
			);
			
			// replace
			$replace = array(
				'>',
			    '<',
			    '\\1',
			    '//<![CDATA['
			);
			
			// run search and replace
			$buffer = preg_replace($search, $replace, $buffer);
			
			// resolve cdata content
			$buffer = str_replace('//<![CDATA[', '//<![CDATA[' . PHP_EOL, $buffer);
		}
		
		// set output
		$CI->output->set_output($buffer);
			
		// display output
		$CI->output->_display();
	}
    
    /**
     * Set title on Layout
     * 
     * @author hungtd
     * 
     * @param string $title title show on web
     */
    public function set_title($title)
    {
        $this->title = $title;
    }
    
    /**
     * @author hungtd
     * 
     * @param string $keyword keyword for website
     */
    public function set_keyword($keyword)
    {
        $this->keyword = $keyword;
    }
    
    /**
     * @author hungtd
     * 
     * @param string $description description for website
     */
    public function set_description($description)
    {
        $this->description = $description;
    }
    
    /**
     * set css file
     * 
     * @param string $style_name 
     */
    public function set_rel($style_name)
    {
        $this->pgRel = $style_name;
    }
    
    /**
     * set javascript type
     * 
     * @param string $javascript_name 
     */
    public function set_javascript($javascript_name)
    {
        $this->pgJavascript = $javascript_name;
    }
    
    /**
     * get title add prefix
     * 
     * @author hungtd
     * 
     * @return string title
     */
    public function get_title()
    {
        if ($this->title)
            return $this->title . ' | ' . config_item('penguin_site_name');
        else return config_item('penguin_site_name');
    }
    
    /**
     * @author hungtd
     * 
     * @return strong keyword
     */
    public function get_keyword()
    {
        if ($this->keyword) return $this->keyword;
        else return config_item('penguin_site_keyword');
    }
    
    /**
     * @author hungtd
     * 
     * @return strong description 
     */
    public function get_description()
    {
        if ($this->description) return $this->description;
        else return config_item('penguin_site_description');
    }
    
    /**
     * get Rel
     * 
     * @return string
     */
    public function get_rel()
    {
        // check invalid rel        
        if ($this->pgRel)
        {                        
            // Nếu là mảng thì add nhiều file css
            if (is_array($this->pgRel))
            {
                $style = '';
                
                foreach ($this->pgRel as $css)
                {
                    $style .= '<link href="'. $this->getLinkStatic('css', $css) .'" rel="stylesheet" type="text/css" />';
                }
                
                // return string add css to view
                return $style;
            } else // là chuỗi thì add 1 file css đến view
            {
                // return string add css to view
                return '<link href="'. $this->getLinkStatic('css', $this->pgRel) .'" rel="stylesheet" type="text/css" />';
            }
        }
        
        return FALSE;
    }
    
    /**
     * get javascript
     * 
     * @return string
     */
    public function get_javascript()
    {
        if ($this->pgJavascript)
        {
            // Nếu là mảng thì add nhiều file javascript
            if (is_array($this->pgJavascript))
            {
                $javascript = '';
                
                foreach ($this->pgJavascript as $js)
                {
                    $javascript .= '<script type="text/javascript" src="'. $this->getLinkStatic('js', $js) .'"></script>';
                }
                
                // return string add javascript to view
                return $javascript;
            } else // là chuỗi thì add 1 file javascript đến view
            {
                // return string add javascript to view
                return '<script type="text/javascript" src="'. $this->getLinkStatic('js', $this->pgJavascript) .'"></script>';
            }
        }
        
        return FALSE;
    }
    
    /**
     * Get link to file in static folder
     * 
     * @param string $subfolder
     * @param string $file
     * @return string 
     */
    public function getLinkStatic($subfolder = '', $file = '')
    {
        // link statis
        $link = base_url() . 'static/' . config_item('penguin_theme');
        
        // get link sub folder
        if ($subfolder)
        {
            $link .= '/'. $subfolder .'/';
        }
        
        // get link file
        if ($file)
        {
            $link .= $file; 
        }
        
        return $link;
    }
}
// END Layout Class

/* End of file Layout.php */
/* Location: ./application/libraries/Layout.php */